package com.kashow.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.kashow.AppContext;
import com.kashow.C;
import com.kashow.R;
import com.orhanobut.logger.Logger;
import com.trello.rxlifecycle.LifecycleProvider;
import com.trello.rxlifecycle.LifecycleTransformer;
import com.trello.rxlifecycle.RxLifecycle;
import com.trello.rxlifecycle.android.FragmentEvent;
import com.trello.rxlifecycle.android.RxLifecycleAndroid;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Observable;
import rx.subjects.BehaviorSubject;

/**
 * Created by liwenyue on 7/13/16.
 */
public abstract  class BaseSupportFragment extends SupportFragment implements Toolbar.OnMenuItemClickListener,LifecycleProvider<FragmentEvent> {
    protected static final String ARGS_PARAM1 = C.ARGS.PARAM1;
    protected static final String ARGS_PARAM2 = C.ARGS.PARAM2;
    protected static final String ARGS_PARAM3 = C.ARGS.PARAM3;
    protected static final String ARGS_PARAM4 = C.ARGS.PARAM4;
    protected Toolbar mToolbar;
    protected View mRootView;
    protected boolean isEnterAnimationFinished;
    private Unbinder mUnBinder;
    private String TAG = getClass().getSimpleName();

    protected String getClassTag() {
        return TAG;
    }

    private final BehaviorSubject<FragmentEvent> lifecycleSubject = BehaviorSubject.create();

    @Override
    @NonNull
    @CheckResult
    public final Observable<FragmentEvent> lifecycle() {
        return lifecycleSubject.asObservable();
    }

    @Override
    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindUntilEvent(@NonNull FragmentEvent event) {
        return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
    }

    @Override
    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindToLifecycle() {
        return RxLifecycleAndroid.bindFragment(lifecycleSubject);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        lifecycleSubject.onNext(FragmentEvent.ATTACH);
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onAttach");
    }




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mRootView =  inflater.inflate(getLayoutId(),null); //inflateView(inflater);
        ButterKnife.bind(this,mRootView);
        Logger.e("init Success  %s",mRootView);

        if (hasToolbar()) {
            mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
            if (mToolbar == null) {
                return mRootView;
            }
            if (getToolbarTitle() != -1) {
                mToolbar.setTitle(getToolbarTitleString());
            }
            if (getMenuId() != -1) {
                initToolbarMenu(mToolbar);
            }
            if (showNavIcon()) {
                initToolbarNav(mToolbar);
            }
        }
        return mRootView;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lifecycleSubject.onNext(FragmentEvent.CREATE);
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onCreate");
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//            Window window = _mActivity.getWindow();
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//                    |WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
//                    |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    |View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.TRANSPARENT);
//            //   window.setNavigationBarColor(Color.TRANSPARENT);
//            //底部导航栏的状态
//        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lifecycleSubject.onNext(FragmentEvent.CREATE_VIEW);
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onViewCreated");
    }


    @Override
    public void onStart() {
        super.onStart();
        lifecycleSubject.onNext(FragmentEvent.START);
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        lifecycleSubject.onNext(FragmentEvent.RESUME);
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onResume");
    }

    @Override
    public void onPause() {
        lifecycleSubject.onNext(FragmentEvent.PAUSE);
        super.onPause();
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onPause");
    }

    @Override
    public void onStop() {
        lifecycleSubject.onNext(FragmentEvent.STOP);
        super.onStop();
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onStop");
    }

    @Override
    public void onDestroyView() {
        lifecycleSubject.onNext(FragmentEvent.DESTROY);
        super.onDestroyView();
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onDestroyView");
    }

    @Override
    public void onDestroy() {
        lifecycleSubject.onNext(FragmentEvent.STOP);
        isEnterAnimationFinished = false;
        super.onDestroy();
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onDestroy");
    }

    @Override
    public void onDetach() {
        lifecycleSubject.onNext(FragmentEvent.DETACH);
        super.onDetach();
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onDetach");
    }

    public void initToolbarMenu(Toolbar toolbar) {
        toolbar.inflateMenu(getMenuId());
        toolbar.setOnMenuItemClickListener(this);
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onSupportInvisible");
    }

    @Override
    public void onSupportVisible() {
        super.onSupportInvisible();
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onSupportVisible");
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
//        switch (item.getItemId()) {
//            case R.id.action_hierarchy:
//                _mActivity.showFragmentStackHierarchyView();
//                break;
//        }
//        return true;
    }

    @Override
    protected void onEnterAnimationEnd(Bundle savedInstanceState) {
        isEnterAnimationFinished = true;
    }

    protected abstract int getLayoutId();

    protected abstract void initEventAndData();

    protected boolean hasToolbar () {
        return true;
    }

    protected boolean showNavIcon() {
        return false;
    }

    protected int getMenuId() {
        return -1;
    }

    public Context getContext() {
        return getActivity();
    }

    protected void initToolbarNav(Toolbar toolbar) {
        toolbar.setNavigationIcon(getNavIcon());
        toolbar.setNavigationOnClickListener(v -> _mActivity.onBackPressed());
    }

    protected int getNavIcon() {
        return R.drawable.ic_arrow_back_default;
    }

    protected int getToolbarTitle() {
        return -1;
    }

    protected String getToolbarTitleString() {
        if (getToolbarTitle()  <= 0) {
            return "";
        }
        return getString(getToolbarTitle());
    }

    public int dip2px(float dip) {
        final float scale = getActivity().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        Logger.d("----fragment----%s----life----->%s-----",this.getClass().getSimpleName(),"onLazyInitView");
    }

    protected void showToast(int strRes, String msg) {
        AppContext.showToast(getString(strRes,msg));
    }

    protected void showToast(int strRes) {
        AppContext.showToast(strRes);
    }

    protected void showToast(CharSequence string) {
        AppContext.showToast(string);
    }

    protected void showToast(String string) {
        AppContext.showToast(string);
    }

}
